<template>
  <view class="container">
    <view class="form-group">
      <text>收货人姓名：</text>
      <input v-model="address.name" type="text" placeholder="请输入姓名">
    </view>
    <view class="form-group">
      <text>电话号码：</text>
      <input v-model="address.phone" type="text" placeholder="请输入电话">
    </view>
    <view class="form-group">
      <text>省市区：</text>
      <input v-model="address.province" type="text" placeholder="省份">
      <input v-model="address.city" type="text" placeholder="城市">
      <input v-model="address.town" type="text" placeholder="区县">
    </view>
    <view class="form-group">
      <text>详细地址：</text>
      <input v-model="address.address" type="text" placeholder="请输入详细地址">
    </view>
	<view class="form-group">
	      <text>设为默认地址：</text>
	      <radio-group @change="setDefaultAddress">
	        <label>
	          <radio value="1" :checked="address.isDefault === 1"/> 是
	        </label>
	        <label>
	          <radio value="0" :checked="address.isDefault === 0"/> 否
	        </label>
	      </radio-group>
	    </view>
	<view class="button-container">
	      <button @click="saveAddress" class="save-button" type="primary">保存</button>
	      <button @click="delAddress" class="delete-button" type="primary">删除</button>
	    </view>
  </view>
</template>

<script>
  import config from '@/config'; // 根据实际情况引入 config

  export default {
    data() {
      return {
        addressid:null,
        address: {
          name: '',
          phone: '',
          province: '',
          city: '',
          town: '',
          address: ''
        }
      };
    },
    onLoad(options) {
      this.addressid = options.addressid; // 使用 options 获取路由参数
      console.log('Address ID:', this.addressid); // 添加这一行来打印地址ID
      this.fetchAddressDetails();
    },
    methods: {
		 setDefaultAddress(e) {
		    this.address.isDefault = parseInt(e.detail.value, 10);
		  },
      saveAddress() {
        uni.request({
            url: `${config.apiUrl}/address/save`,
            method: 'POST',
            data: this.address,
            success: (response) => {
              // 处理成功响应
              console.log('Address saved successfully:', response.data);
              // 如果设置了默认地址，发送请求到 setdefault 端点
              if (this.address.isDefault) {
                uni.request({
                  url: `${config.apiUrl}/address/setdefault/${this.addressid}`,
                  method: 'POST',
                  success: (setDefaultResponse) => {
					  console.log("addressid：" +this.addressid+" isDefult： "+this.address.isDefault)
                    console.log('Address set as default successfully:', setDefaultResponse.data);
                  },
                  fail: (error) => {
                    console.error('Error setting address as default:', error);
                  }
                });
              }
            },
            fail: (error) => {
              // 处理错误响应
              console.error('Error saving address:', error);
            }
          });
          uni.navigateTo({
            url:'/pages/mine/address/index2'
          });
      },
	  delAddress(){
		  uni.showModal({
		          title: '确认',
		          content: '确定要删除这个地址吗？',
		          success: (res) => {
		              if (res.confirm) {
		                  uni.request({
		                      url: `${config.apiUrl}/address/deleteAddress/${this.addressid}`,
		                      method: 'DELETE',
		                      success: (response) => {
		                          // 处理成功响应
		                          console.log('Address deleted successfully:', response.data);
		                          // 可能需要跳转到地址列表页面或其他操作
		                          uni.redirectTo({
		                              url: '/pages/mine/address/index' // 替换为您的地址列表页面路由
		                          });
		                      },
		                      fail: (error) => {
		                          // 处理错误响应
		                          console.error('Error deleting address:', error);
		                      }
		                  });
		              }
		          }
		      });
		          },
		         
      fetchAddressDetails() {
        uni.request({
          url: `${config.apiUrl}/address/details/${this.addressid}`,
          method: 'GET',
          data: { id: this.addressid }, // 确保这里的 this.addressid 是正确的
          success: (response) => {
            this.address = response.data;
          },
          fail: (error) => {
            console.error('Error fetching address details:', error);
          }
        });
      }
    }
  };
</script>

<style scoped>
  .container {
    padding: 20px;
    background-color: #ffefba;
    height: 550px;
  }
  .form-group {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-bottom: 40px; /* 增加每个输入组之间的间距 */
  }
  .form-group text {
    margin-right: 10px;
  }
  .input-container {
    background-color: #f0f0f0; /* 浅灰色背景 */
    border-radius: 10px; /* 圆角 */
    padding: 10px; /* 内边距 */
  }
  .form-group input {
    flex: 1;
    margin-right: 10px;
  }
  .button-container {
    display: flex;
    justify-content: space-between; /* 添加这个样式来调整按钮之间的间距 */
    margin-top: 20px; /* 添加这个样式来增加按钮与输入框之间的间距 */
  }
  .save-button {
    background-color: #007bff; /* 保存按钮的背景颜色 */
    color: white;
  }
  .delete-button {
    background-color: #ffcccb; /* 删除按钮的背景颜色，浅红色 */
    color: white;
  }
</style>
